
var vm = new Vue({
    el: "#shopVue",
    data: {
        lists: [],       //商品列表
        supplier:[],     //商户信息
        categories:[],   //分类列表
        brands:[],       //品牌列表
        score:[],        //评分
        type: false,
        brand: false,
        store: true,
        point: false,
        login: true,
        logout: false,
        show:false,
        loading: true,
        product_name:'',
        collect_message:'',
        collect_message_english:'',
        collect_supplier_message:'',
        collect_supplier_message_english:'',
        point_id: '',
        user_id:'',
        web_token:'',
        city:'',
        searchs: '',
        supplier_id:'',
        pageNo: 1,      //商品页码
        paginate:'',
        page_no: 1,     //评论页码
        html: '',
        dataPost: '',
        classify: '',
        classify_id:'',
        changes:{
            type:''
        },
        oss_url:oss_url,
        nodata:'',
        tel:'',
        email:'',
        classification_id:'',
        brand_id:''
    },
    methods: {
        dataDetails:function (id) {
            this.type           = true;
            this.classify_id    = id;
        },
        hiddenDetail:function () {
            $('.categories_list').find('li').children('a').removeClass('active');
            $('.categories_list').find('li').find('img').attr('src',"/image/userweb/slicesImg/list-arrow.png");
            this.type           = false;
        },
        search: function () {
            this.pageNo     = 1;
            this.classify   = '';
            this.req();
        },
        classification:function (index,id) {
            // this.store = false;
            if(index == 1){
                this.brand      = false;
                this.type       = false;
                this.store       = false;
                this.classify   = 'category_id,'+id;
                this.classification_id = 'category_id,'+id;
            }
            if(index == 2){
                this.brand      = false;
                this.type       = false;
                this.store       = false;
                this.classify   = 'type_id,'+id;
                this.classification_id = 'type_id,'+id;
            }
            if(index == 3){
                this.classify   = 'small_type_id,'+id;
                this.classification_id = 'small_type_id,'+id;
                this.brand      = true;
                this.type       = false;
                this.store       = false;
            }
            if(index == 4){
                this.brand_id   = ',brand_id,'+id;
                this.classify   = this.classification_id+this.brand_id;
            }
            if (index != 4){
                if (this.classify){
                    var leng = this.classify .lastIndexOf("\,");
                    if (leng>0){
                        this.point_id   = this.classify .substring( leng + 1, this.classify .length);
                        this.point      = true;
                    }
                }
            }
            if (index == 5){
                $('.categories_list').find('li').children('a').removeClass('active');
                $('.categories_list').find('li').find('img').attr('src',"/image/userweb/slicesImg/list-arrow.png");
                this.classify   = '';
                this.brand      = false;
                this.type       = false;
                this.store       = true;
            }
            this.searchs    = '';
            this.pageNo     = 1;
            this.req();
        },
        confirm: function (index) {
            if (!this.user_id){
                location.href = "/userweb/index/login.html";
            }
            validToken(); //验证tonken.
            if (index == 1){
                var url = '/api/Collection/confirmCollectSupplier';
                var data = {
                    user_id:        this.user_id,
                    web_token:      this.web_token,
                    supplier_id:    this.supplier_id
                };
                this.$http.post(url, data, {
                    emulateJSON: true
                }).then(function (res) {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }, function (res) {

                });
                this.ini();
                this.req();
            }
        },
        collect: function (index,list) {
            if (!this.user_id ){
                location.href = "/userweb/index/login.html";
            }
            validToken(); //验证tonken.
            if (index == 1){
                var url = '/api/Collection/confirmCollectProduct';
                this.product_name               = list.product_name;
                this.collect_message            = '收藏成功，您可以在收藏夹中查看该商品';
                this.collect_message_english    = 'Collection success, you can view this product in the favorites';
            }
            if (index == 2){
                var url = '/api/Collection/delProduct';
                this.product_name               = list.product_name;
                this.collect_message            = '取消收藏成功';
                this.collect_message_english    = 'Cancel Collection success';
            }
            var data = {
                user_id:        this.user_id,
                web_token:      this.web_token,
                product_id:     list.id
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                //layer.msg(res.data.msg,{
            //     skin:'winning-class',
            //         time:3000
            // });
            }, function (res) {

            });
            this.req();
        },
        req: function () {
            this.loading    = true;
            var url = '/userweb/Homepage/getProductAllList';
            var data = {
                user_id:        this.user_id,
                search:         this.searchs,
                supplier_id:    this.supplier_id,
                classify:       this.classify,
                page:           this.pageNo
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.lists      = res.data.product.data;
                    this.brands     = res.data.brands;
                    this.paginate   = res.data.paginate;
                    if (this.brands.length==0){
                        this.brand = false;
                    }else {
                        this.brand = true;
                    }
                    if (this.lists.length==0){
                        $('#nullData').html(this.nodata);
                    }else {
                        $('#nullData').html("");
                    }
                    var _this = this;
                    $.each(this.lists,function (i,v) {
                        _this.lists[i]['product_image'] = _this.oss_url + v['product_image'];
                    });
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
                /*var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                }, 300);*/
                this.loading = false;
            }, function (res) {

            });
        },
        change : function (index) {
            this.loading    = true;
            var url = '/api/Store/storeComment';
            if ( index == 1 ) {
                this.changes.type = '1';
                this.page_no = 1;
            } else if ( index == 2 ) {
                this.changes.type = '2';
                this.page_no = 1;
            } else if ( index == 3 ) {
                this.changes.type = '3';
                this.page_no = 1;
            }
            var data = {
                user_id:        this.user_id,
                web_token:      this.web_token,
                page_no:        this.page_no,
                type :          this.changes.type,
                supplier_id:    this.supplier_id,
                is_web:         1
            };
            this.$http.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                if (res.data.status == 200) {
                    this.score  = res.data.comment.data;
                    if (this.html){
                        this.html   = res.data.html.replace(/jump/, "JumTwo");
                        this.html   = this.html.replace(/page_no/, "pageNo");
                    }
                }
                if (this.score.length==0){
                    $('#noData').html(this.nodata);
                }else {
                    $('#noData').html("");
                }
               /* var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                }, 300);*/
               this.loading = false;
            } , function ( res ) {

            } );
        },
        ini: function () {
            var url = '/api/Store/storeBasic';
            var data = {
                user_id:        this.user_id,
                city:           this.city,
                web_token:      this.web_token,
                supplier_id:    this.supplier_id
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.supplier       = res.data.supplier;
                    this.supplier.star  = star(Math.round(this.supplier.star * Math.pow(10, 0)) / Math.pow(10, 0));
                    this.supplier.image = this.oss_url + this.supplier.image;
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
                if (this.supplier.is_collect == 1){
                    this.collect_supplier_message            = '已收藏该店铺';
                    this.collect_supplier_message_english    = 'Already collection the store';
                }
                if (this.supplier.is_collect == 0){
                    this.collect_supplier_message            = '确认收藏该店铺？';
                    this.collect_supplier_message_english    = 'Confirm to store the store?';
                }
                // console.log(this.supplier);
            }, function (res) {

            });
        },
        category: function () {
            var url = '/userweb/Homepage/getIni';
            var data = {
                supplier_id:    this.supplier_id
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.categories = res.data.categories;
                }
            }, function (res) {

            });
        },
        showSelect:function(){
            if(this.show == false){
                $('.select_search_img').find('img').css('transform','rotate(180deg)');
                $('#baseData').css('display','block');
                this.show = true;
            }else {
                $('.select_search_img').find('img').css('transform','rotate(360deg)');
                $('#baseData').css('display','none');
                this.show = false;
            }
        },
        xhlData:function () {
            var url = '/userweb/Homepage/xhlData';
            var data = {
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.tel    = res.data.xhlData.tel;
                    this.email  = res.data.xhlData.email;
                }
            }, function (res) {

            });
        }
    },
    mounted: function () {
        this.nodata += '<div class="my_no-data">';
        this.nodata += '<div class="no_data_tip">';
        this.nodata += '<img src="/image/userweb/slicesImg/no-data.png" alt="">';
        this.nodata += '<div class="no-data-text">';
        this.nodata += '<div class="title_zh">暂无数据</div>';
        this.nodata += '<div class="title_en">No data</div>';
        this.nodata += '</div>';
        this.nodata += '</div>';
        this.nodata += '</div>';
        var str             = location.href; //取得整个地址栏
        this.supplier_id    = getUrlParam(str,'supplierId');
        this.searchs        = getUrlParam(str,'search');
        this.classify       = getUrlParam(str,'classify');
        this.classification_id       = getUrlParam(str,'classify');
        if (this.classify == '#'){
            this.classify = '';
        }
        if (this.classification_id == '#'){
            this.classification_id = '';
        }
        this.user_id        = localStorage.getItem('user_id');
        this.web_token      = localStorage.getItem('web_token');
        this.city           = localStorage.getItem('city');
        if (!this.city){
            this.city       = 'Devon County';
        }
        if (this.user_id){
            this.login  = false;
            this.logout = true;
        }else {
            this.login  = true;
            this.logout = false;
        }
        var index           = str .lastIndexOf("\,");
        if (index>0){
            this.point      = true;
            this.point_id   = str .substring(index + 1, str .length); //截取地址栏上的类别id
        }
        this.req();
        this.ini();
        this.category();
        this.xhlData();
    }
});

function AjaxPage(page) {
    vm.pageNo = page;
    vm.req();
}
function jump(){
    var page = $("#page_no").val();
    vm.pageNo = page;
    vm.req();
}
function JumTwo(){
    var page_no = $("#pageNo").val();
    vm.page_no   = page_no;
    vm.change();
}
function ajax_page(page) {
    vm.page_no = page;
    vm.change();
}
